package com.block.channelext.service.channel;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.channelext.model.result.ReportResult;
import com.block.channelext.service.IChannelHandleService;
import com.block.common.service.IChannelCommonService;
import com.block.common.service.LogFactory;
import com.block.db.entity.ChannelInfo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class XiangDianFlowChannelHandleService implements IChannelHandleService {

    private String requestUrl = "https://oicount.adexplain.com/v2/action/monitor";

    private static Logger log = LogFactory.getCommLog();

    @Override
    public ReportResult registerAction(ChannelInfo channelInfo, JSONObject extJson) {

        String landingid = extJson.getString("landingid");
        if (StrUtil.isEmpty(landingid)){
            return ReportResult.newInstance();
        }

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("landingid", landingid);
        bizMap.put("exposure", 0);

        log.info("【享点上报】【注册】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String response = HttpUtil.createGet(requestUrl)
                .form(bizMap)
                .execute().body();
        log.info("【享点上报】【注册】【开始结束】【result】{}", response);

        ReportResult reportResult = ReportResult.newInstance(JSON.toJSONString(bizMap), response);
        JSONObject jsonObject = JSONObject.parseObject(response);
        boolean isSuccessed = jsonObject.getBoolean("isSuccessed");
        String errorCode = jsonObject.getString("errorCode");
        String errorDesc = jsonObject.getString("errorDesc");
        reportResult.setCode(errorCode);
        reportResult.setMessage(errorDesc);
        if (isSuccessed){
            reportResult.setSuccess(true);
        }
        return reportResult;
    }

    @Override
    public ReportResult authAction(ChannelInfo channelInfo, JSONObject extJson) {

        return ReportResult.newInstance();
    }

    @Override
    public ReportResult proUvAction(ChannelInfo channelInfo, JSONObject extJson) {


        return ReportResult.newInstance();
    }

}
